/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rbh.risk.service.impl;

import com.rbh.risk.dao.RiskDataDao;
import com.rbh.risk.entity.Privilege;
import com.rbh.risk.entity.RiskData;
import com.rbh.risk.entity.SeverityLevel;
import com.rbh.risk.entity.Workgroup;
import com.rbh.risk.model.SummaryLevelModel;
import com.rbh.risk.service.AuthenticationService;
import com.rbh.risk.service.RiskDataService;
import com.rbh.risk.util.Constants;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("riskDataService")
public class RiskDataServiceImpl implements RiskDataService {

    private final static Logger logger = Logger.getLogger(RiskDataService.class);
    @Autowired
    private RiskDataDao riskDataDao;
    @Autowired
    private AuthenticationService authenticationService;

    @Transactional(readOnly = false)
    @Override
    public void insert(RiskData risk) {
        riskDataDao.insert(risk);
        logger.info("Insert risk by " + risk.getProfileCreate().getProfileId());
    }

    @Transactional(readOnly = false)
    @Override
    public void update(RiskData risk) {
        riskDataDao.update(risk);
        logger.info("Update riskId [" + risk.getDataId() + "]");
    }

    @Transactional(readOnly = false)
    @Override
    public void delete(RiskData risk) {
        logger.info("Delete riskId [" + risk.getDataId() + "]");
        riskDataDao.delete(risk);
    }

    @Transactional(readOnly = true)
    @Override
    public RiskData getById(String riskId) {
        return riskDataDao.getById(riskId);
    }

    @Transactional(readOnly = true)
    @Override
    public List<RiskData> getAll() {
        return riskDataDao.getAll();
    }

    @Transactional(readOnly = true)
    @Override
    public List<RiskData> getByProfile(HashMap<Privilege, String> privileges, Date dateFrom, Date dateTo) {
        return riskDataDao.getByProfile(privileges, authenticationService.getPrincipal().getProfile(), dateFrom, dateTo);
    }

    @Transactional(readOnly = true)
    @Override
    public List<SummaryLevelModel> getMinorSummaryLevels(Workgroup workgroup, Date from, Date to) {
        return riskDataDao.getMinorSummaryLevels(workgroup, from, to);
    }

    @Transactional(readOnly = true)
    @Override
    public List<SummaryLevelModel> getMajorSummaryLevels(HashMap<Privilege, String> privileges, Date from, Date to) {
        return riskDataDao.getMajorSummaryLevels(privileges, authenticationService.getPrincipal().getProfile(), from, to);
    }

    @Transactional(readOnly = true)
    @Override
    public List<RiskData> getListRiskDetail(HashMap<Privilege, String> privileges, Workgroup workgroup, SeverityLevel level, Date from, Date to) {
        return riskDataDao.getRiskDetailList(workgroup, level,
                (privileges.containsValue(Constants.$_YOURSELF) ? authenticationService.getPrincipal().getProfile() : null), from, to);
    }

    @Transactional(readOnly = true)
    @Override
    public List<RiskData> getByFilter(int first, int pageSize, String sortField, boolean sortOrder, Map<String, String> filters) {
        return riskDataDao.getByFilter(first, pageSize, sortField, sortOrder, filters, null, null);
    }
    
    @Transactional(readOnly = true)
    @Override
    public Integer getMaxResult(Map<String, String> filters) {
        return riskDataDao.getMaxResult(filters, null, null);
    }
}
